package com.chaos.payment.controller;

import com.chaos.framework.starter.base.BaseController;
import io.swagger.annotations.Api;
import lombok.extern.slf4j.Slf4j;
import org.apache.commons.io.IOUtils;
import org.apache.poi.ss.formula.functions.T;
import org.springframework.cloud.context.config.annotation.RefreshScope;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestBody;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.RestController;

import javax.servlet.http.HttpServletRequest;

/**
 * @description:
 * @author: crayon
 * @time: 2024/6/25 11:25
 */

@Slf4j
@RestController
@Api(tags = "微信回调")
@RequestMapping("/wx/notify")
@RefreshScope
public class WxOrderController extends BaseController<T> {

//    @Value("${lcw.host}")
//    private  String redisConfig;
//
//    @PostMapping("/test")
//    public void test(){
//        log.info("host:{}",redisConfig);
//    }
    /**
     * 支付回调
     *
     * @param requestBody
     * @param request
     * @return
     */
    @PostMapping("/pay")
    public String payNotify(@RequestBody String requestBody, HttpServletRequest request) {
        try {
            String xmlResult = IOUtils.toString(request.getInputStream(), request.getCharacterEncoding());
            log.info("微信支付回调通知结果：" + xmlResult);
//            WxPayOrderNotifyResult result = wxPayService.parseOrderNotifyResult(xmlResult);
//
//            // 加入自己处理订单的业务逻辑，需要判断订单是否已经支付过，否则可能会重复调用
//            String orderId = result.getOutTradeNo();
//            String tradeNo = result.getTransactionId();
//            String totalFee = BaseWxPayResult.fenToYuan(result.getTotalFee());
           // return WxPayNotifyResponse.success("处理成功!");
        } catch (Exception e) {
            log.error("微信回调结果异常,异常原因{}", e.getMessage());
           // return WxPayNotifyResponse.fail(e.getMessage());
        }
        return "";
    }

    /**
     * 信用分支付回调
     *
     * @param requestBody
     * @param request
     * @return
     */
    @PostMapping("/payScore")
    public String payScoreNotify(@RequestBody String requestBody, HttpServletRequest request) {
        try {
            String xmlResult = IOUtils.toString(request.getInputStream(), request.getCharacterEncoding());
            log.info("微信支付回调通知结果：" + xmlResult);
//            WxPayOrderNotifyResult result = wxPayService.parseOrderNotifyResult(xmlResult);
//
//            // 加入自己处理订单的业务逻辑，需要判断订单是否已经支付过，否则可能会重复调用
//            String orderId = result.getOutTradeNo();
//            String tradeNo = result.getTransactionId();
//            String totalFee = BaseWxPayResult.fenToYuan(result.getTotalFee());
            //return WxPayNotifyResponse.success("处理成功!");
        } catch (Exception e) {
            log.error("微信回调结果异常,异常原因{}", e.getMessage());
            //return WxPayNotifyResponse.fail(e.getMessage());
        }
        return "";
    }
    /**
     * 退款回调 信用分和支付是同一个
     *
     * @param requestBody
     * @param request
     * @return
     */
    @PostMapping("/refund")
    public String refundNotify(@RequestBody String requestBody, HttpServletRequest request) {
        try {
            String xmlResult = IOUtils.toString(request.getInputStream(), request.getCharacterEncoding());
            log.info("微信支付回调通知结果：" + xmlResult);
//            WxPayOrderNotifyResult result = wxPayService.parseOrderNotifyResult(xmlResult);
//
//            // 加入自己处理订单的业务逻辑，需要判断订单是否已经支付过，否则可能会重复调用
//            String orderId = result.getOutTradeNo();
//            String tradeNo = result.getTransactionId();
//            String totalFee = BaseWxPayResult.fenToYuan(result.getTotalFee());
        //    return WxPayNotifyResponse.success("处理成功!");
        } catch (Exception e) {
            log.error("微信回调结果异常,异常原因{}", e.getMessage());
        //    return WxPayNotifyResponse.fail(e.getMessage());
        }
        return "";
    }


}
